package cn.youruan.demo.service.impl;



import cn.youruan.demo.domain.User;
import cn.youruan.demo.dto.UserDto;
import cn.youruan.demo.mapper.UserMapper;
import cn.youruan.demo.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    /**
     * 根据用户id查询用户拥有的爱好个数
     * @param userId
     * @return
     */
    @Autowired
    private UserMapper userMapper;

    @Override
    public Integer selectHobbyCount(Long userId) {
        return userMapper.selectHobbyCount(userId);
    }

    /**
     * 根据用户id和爱好id添加中间表关系
     * @param userId
     * @param hobbyId
     */
    @Override
    public void addHobbyRelation(Long userId, Long hobbyId) {
        userMapper.addHobbyRelation(userId,hobbyId);
    }

    @Override
    public boolean isHobby(Long userId) {
        Integer count = userMapper.selectHobbyCount(userId);
        return count > 0;
    }

    @Override
    public Long selectHobbyIdByUserId(Long userId) {
        return userMapper.selectHobbyIdByUserId(userId);
    }

    @Override
    @Transactional
    public void deleteHobbyRelation(Long userId,Long hobbyId) {
        userMapper.deleteHobbyRelation(userId,hobbyId);
    }

    @Override
    public boolean isFinishUserInfo(User user) {
        String headImg = user.getHeadImg();
        if (headImg==null) {
            return false;
        }
        String name = user.getName();
        if (name==null) {
            return false;
        }
        LocalDateTime birthday = user.getBirthday();
        if (birthday==null) {
            return false;
        }
        Integer count = userMapper.selectHobbyCount(user.getId());
        if (count<2) {
            return false;
        }
        return true;
    }

    @Override
    public List<UserDto> sortByHobbyMaxUser() {
        return userMapper.sortByHobbyMaxUser();
    }

    @Override
    public List<User> sortByCount() {
        return userMapper.sortByCount();
    }
}
